// pages/admin/putIn/dataGraph/dataGraph.js
const app = getApp();
var wxCharts = require('../../../../utils/wxcharts.js');
var util = require('../../../../utils/util.js');
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classification:[],
    currMonth: '本月',
    chooseMonth: '',//选中的月份
    endMonth: util.formatMonth(new Date),//限制不可选取未来的月份
    code:null,
  },

  //选择时间
  bindMonthPicker: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var chooseMonth = e.detail.value;
    if( chooseMonth == util.formatMonth(new Date)){
      chooseMonth = '本月'
    }
    this.setData({
      currMonth: chooseMonth
    });
    this.getMonthForm(e.detail.value);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that=this;
   that.getMonthForm();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //请求月份
  getMonthForm:function(value){
    var that=this;
    var uid=wx.getStorageSync('userid');
    var param={
      uid:uid.id,
      type:1,
      selType:3,
      time:value
    }
    console.log(param)
    Api.ajax('/app/Zmanage/dataForm',param).then(res=>{
      console.log(res)
      if (res.code==200) {
        that.setData({
          classification:res.data
        })
        that.getClassification();
      }else{
        that.setData({
          classification:res.data
        })
      }

    })
  },
  //分类canvas图形
  getClassification:function(){
    var that = this;
    // 历史数据
    let classification = that.data.classification;
    var weight= [];
    var title = [];
    console.log(classification)
    for (var i in classification) {
      var obj = {
        weight: classification[i].weight,
        title: classification[i].title
      }
      weight = weight.concat(obj.weight);
      //console.log(month);
      title = title.concat(obj.title);
      // console.log(datas);
    }
    that.setData({
      weights: weight,
      title: title,
    });
    new wxCharts({
      canvasId: 'dataGraph',
      type: 'column',
      categories: title,
      series: [
        {
          name: ' ',
          data: weight,
          format: function (val, name) {
            return val + 'kg';
          }
        }
      ],
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      yAxis: {
        min: 0,
      },
      extra: {
        column: {
          width: 20
        }
      },
      width: 350,
      height: 300,
      dataLabel: true,
      legend: false
    });
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})